import { request } from "../utils"
import { Toast } from "vant"
import { format, addDays } from "date-fns"

/*
  获取主理人信息
*/
const getBelongInfo = (requestData, cb) => {
  request({
    url: "api/Distribution/GetDistributor",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      cb && cb(res.data.Data)
    } else {
      console.log("获取主理人信息失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  是否可申请成为主理人
*/
const getCanApply = (requestData, cb) => {
  request({
    url: "api/Distribution/GetCanApplyMyShop",
    params: requestData
  }).then(res => {
    let flag = res.data.Success
    let msg = res.data.Msg
    cb && cb(flag, msg)
  })
}

/* 
  申请成为主理人
*/
const applyBelong = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Distribution/PostApplyDistributor",
    data: requestData
  }).then(res => {
    if (
      res.data.Success ||
      res.data.Msg === "您己经提交销售员申请，请耐心等待！"
    ) {
      cb && cb()
    } else {
      console.log("申请成为主理人失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取主理人店铺基础信息
*/
const getMyShop = (requestData, cb) => {
  request({
    url: "api/Distribution/GetMyShop",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let shop = res.data.Data
      cb && cb(shop)
    } else {
      console.log("获取主理人店铺信息失败...")
    }
  })
}

/*
  获取主理人佣金
*/
const getMyBenefit = (requestData, cb) => {
  request({
    url: "api/Distribution/GetMyBrokerage",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let benefit = res.data.Data
      cb && cb(benefit)
    } else {
      console.log("获取主理人收益信息失败...")
    }
  })
}

/*
  获取主理人佣金流水记录
*/
const getMyBenefitList = (requestData, cb) => {
  request({
    url: "api/Distribution/GetRecords",
    params: requestData
  }).then(res => {
    if (res.data.Success) {
      let list = res.data.Data
      cb && cb(list)
    } else {
      console.log("获取主理人收益列表失败...")
    }
  })
}

/* 
  主理人佣金提现
  Type WeChat Alipay UnionCard // 2 3 4
*/
const benefitWithdraw = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Distribution/PostWithdraw",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("提现申请失败...")
      Toast(res.data.Msg)
    }
  })
}

/* 
  主理人佣金提现 - 订单形式全额提
*/
const benefitWithdrawIds = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Distribution/PostWithdrawOrders",
    data: requestData,
  }).then((res) => {
    if (res.data.Success) {
      cb && cb()
    } else {
      console.log("提现申请失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取主理人佣金订单记录
*/
const getMyBenefitOrders = (cb) => {
  request({
    url: "api/Distribution/GetShopOrder",
    params: {
      userkey: localStorage.getItem("userKey"),
      pageNo: 1,
      pageSize: 10,
    },
  }).then((res) => {
    if (res.data.success) {
      let list = res.data.data.rows
      list.map(item => {
        item.OrderDate = format(new Date(item.OrderDate), "yyyy-MM-dd")
        item.OrderGetDate = format(
          addDays(new Date(item.OrderDate), 7),
          "yyyy-MM-dd"
        )
        return item
      })
      cb && cb(list)
    } else {
      console.log("获取主理人佣金订单失败...")
    }
  })
}

/*
  获取主理人粉丝列表
*/
const getMyFans = cb => {
  request({
    url: "api/Distribution/GetMySubordinate",
    params: {
      userkey: localStorage.getItem('userKey')
    },
  }).then((res) => {
    if(res.data.Success !== false){
      cb && cb(res.data)
    } else {
      console.log("获取主理人粉丝失败...")
    }
    // if (res.data.Success) {
    //   let list = res.data.Data
    //   cb && cb(list)
    // } else {
    //   console.log("获取主理人粉丝失败...")
    // }
  })
}

/* 
  银行卡绑定
*/
const bindBankCard = (requestData, cb) => {
  request({
    method: "POST",
    url: "api/Distribution/PostBankCard",
    data: requestData,
  }).then((res) => {
    if (res.data.success) {
      cb && cb()
    } else {
      console.log("绑定银行卡失败...")
      Toast(res.data.Msg)
    }
  })
}

/*
  获取银行卡号
*/
const getBankCard = (cb) => {
  request({
    url: "api/Distribution/GetMyPayInfo",
    params: {
      userkey: localStorage.getItem('userKey')
    },
  }).then((res) => {
    if (res.data.success) {
      let list = res.data.data.card
      cb && cb(list)
    } else {
      console.log("获取银行卡失败...")
    }
  })
}

/* 
  删除银行卡
*/
const deleteBankCard = (requestData, cb) => {
  request({
    method: "Delete",
    url: "api/Distribution/DelOneCard",
    params: requestData,
  }).then((res) => {
    if (res.data.success) {
      cb && cb()
    } else {
      console.log("删除银行卡号失败...")
      Toast(res.data.msg)
    }
  })
}

export {
  getBelongInfo,
  getCanApply,
  applyBelong,
  getMyShop,
  getMyBenefit,
  getMyBenefitList,
  getMyFans,
  getMyBenefitOrders,
  bindBankCard,
  getBankCard,
  deleteBankCard,
  benefitWithdraw,
  benefitWithdrawIds,
}
